package ij.umasuo.gui.working;

import ij.IJ;
import ij.ImagePlus;
import ij.umasuo.MyResource;
import ij.umasuo.AOTFController.CtrInterface;
import ij.umasuo.AOTFController.LiveCtr;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;
import java.awt.Color;

/**
 * this class used to viwer the 
 * @author linshan
 *
 */
public class LiveViewer extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private static LiveViewer instance = null;
	
	private ImagePlus imp = null;
//	private ImageCanvas canvas = null;
	private MyImageCanvas canvas = null;
	
	private JPanel northPanel = new JPanel();
	private JPanel southPanel = new JPanel();
	private JPanel centerPanel = new JPanel();
	private boolean isLiving = false;
	private LiveThread liveThread = null;
	
	JButton btnLive = new JButton("Live");
	private LiveViewer(){

		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				resized();
			}
		});
		this.setLayout(null);
		canvas = new MyImageCanvas();
		centerPanel.setBounds(20, 30, 410, 210);
		centerPanel.setLayout(new BorderLayout(0, 0));
		this.add(centerPanel,BorderLayout.CENTER);
		centerPanel.add(canvas,BorderLayout.CENTER);
//		centerPanel.setBackground(Color.red);
		
		northPanel.setBounds(0, 0, 450, 30);
		this.add(northPanel,BorderLayout.NORTH);
		southPanel.setBackground(Color.LIGHT_GRAY);
		southPanel.setBounds(0, 250, 450, 50);
		this.add(southPanel,BorderLayout.SOUTH);
		southPanel.setLayout(null);
		

		btnLive.setIcon(new ImageIcon(MyResource.getCamera_go()));
		btnLive.addActionListener(this);
		btnLive.setBounds(10, 5, 115, 23);
		southPanel.add(btnLive);
	}

	
	/**
	 * get the instance
	 * @return
	 */
	public static LiveViewer getInstance(){
		if(instance == null){
			instance = new LiveViewer();
		}
		return instance;
	}
	
	/**
	 * start the live model, call the controller to start the camera, and here we start a new thread to update the interface
	 */
	public void startLiveModel(){
		LiveCtr.getInstance().startLiveModel();
		isLiving = true;
		btnLive.setIcon(new ImageIcon(MyResource.getCancel()));
		btnLive.setText("Stop Live");
		liveThread = new LiveThread();
		liveThread.start();
	}
	public void stopLiveModel(){
		isLiving = false;
		btnLive.setIcon(new ImageIcon(MyResource.getCamera_go()));
		btnLive.setText("Live");
		LiveCtr.getInstance().stopLiveModel();
	}
	/**
	 * draw image
	 */
	public void drawImage(ImagePlus imp){
		if(imp == null){
			return;
		}
		LiveViewer.getInstance().canvas.drawImage(imp.getImage());
	}
	
	public void resized(){
		
		int height = this.getHeight();
		int width = this.getWidth();
//		System.out.println(width + "  o " + height);
		northPanel.setBounds(0, 0, width, 30);
		southPanel.setBounds(0, height-60, width, 60);
		height = height - 90;
		int gapH = 0,gapV = 0;
		if(width>height){
			gapH = width - height;
			centerPanel.setBounds(gapH/2, 30, width-gapH, height);
		}else{
			gapV = height - width;
			centerPanel.setBounds(0, 30+gapV/2, width, height-gapV);
		}		
		canvas.repaint();

	}

	private class LiveThread extends Thread{
		@Override
		public void run() {
			try{
				while(LiveViewer.getInstance().isLiving){
					Image img = LiveCtr.getInstance().getImage();
					if(img != null){
						LiveViewer.getInstance().canvas.drawImage(img);
					}else{
						Thread.currentThread().sleep(500);
					}
					Thread.currentThread().sleep(20);//this set the frame rate to 50 
				}
			}catch (Exception e){
				LiveViewer.getInstance().stopLiveModel();
				IJ.error("living model error",e.toString());
			}
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(isLiving){
			stopLiveModel();
		}else{
			startLiveModel();
		}
		
	}
}
